/*
 * This file is part of PDBEdit.
 * 
 * PDBEdit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * PDBEdit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PDBEdit.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 13/05/2011
 * Copyright (c) Guerfyld 2010. All rights reserved.
 * http://......
 */
package pdbfile;

/**
 * The PDBLink class manage PilotDB Link field
 *
 * @author guerfyld
 * @version 1.0
 */
public class PDBLink extends PDBString {
	private String lnkFilename;
	private int lnkColumn;
	private long lnkRow;

	/**
	 * Constructor
	 *
	 * @param val	pilotDB field raw value
	 */
	public PDBLink(String filename, int col) {
		super(null);
		lnkFilename = filename;
		lnkColumn = col;
		lnkRow = 0;
	}

	public PDBLink(long row, String val) {
		super(val);
		lnkFilename = null;
		lnkColumn = 0;
		lnkRow = row;
	}

	/**
	 * Constructor
	 *
	 * @param stm	Stream to read
	 * @param pos	Start read position in stream
	 */
	public PDBLink(PDBInputStream stm, long pos) throws Exception {
		super(null);
		lnkFilename = null;
		lnkColumn = 0;
		lnkRow = (int)stm.readValue(pos, 4);
		setValue(stm.readStringZ(pos+4));
	}

	/**
	 * Set link row
	 *
	 * @param off	
	 */
	public void setLnkRow(long row) {
		lnkRow = row;
	}

	/**
	 * Get link filename
	 *
	 * @return	
	 */
	public String getLnkFilename() {
		return lnkFilename;
	}

	/**
	 * Get link column
	 *
	 * @return	
	 */
	public int getLnkColumn() {
		return lnkColumn;
	}

	/**
	 * Get link row
	 *
	 * @return	
	 */
	public long getLnkRow() {
		return lnkRow;
	}

	/**
	 * Returns length of raw data
	 */
	public long length() {
		// PDB Link are StringZ
		// Preceeded with row number (4 bytes)
		return  4 + (strVal.length()+1);
	}

	/**
	 * Clone object
	 * @return	New object
	 */
	public PDBType clone() {
		PDBLink fld = new PDBLink(lnkFilename, lnkColumn);
		fld.setLnkRow(lnkRow);
		fld.setValue(strVal);
		return fld;
	}

	/**
	 * Write fields to stream
	 *
	 * &param stm	Stream to write
	 */
	public void write(PDBOutputStream stm) throws Exception {
		stm.writeValue(lnkRow, 4);
		stm.writeString(getValue());
	}
}